package Server.Model.Base;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class NotifierBase {
	private Thread _thread;
	private Queue<INotifierBase> _queue;
	public NotifierBase(){
		_queue = new LinkedBlockingQueue<INotifierBase>();
		_thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){
					INotifierBase msg = poll();
					if (msg != null) {
						try {
							msg.run();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}else{
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		});  
		_thread.start();  
	}
	public int Count()
    {
       return _queue.size();
    }
	private INotifierBase poll()
    {
		return _queue.poll();
    }
	public void Notify(INotifierBase msg){
		synchronized (this) {
			_queue.add(msg);
		}
	}
	
}
